﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Frogism.Core;

namespace Frogism.Web.Models
{

    public class BookViewData : IBookViewData
    {

        public BookViewData(IEnumerable<IBook> books)
        {
            _Books = books;
        }
        private IEnumerable<IBook> _Books;
        public IEnumerable<IBook> Books
        {
            get { return _Books; }
        }


        public int TotalPages
        {
            get { return _Books.Sum(a => a.TotalPages); }
        }

        public int TotalRead
        {
            get { return _Books.Sum(a => a.TotalRead); }
        }

        public int TotalYearRead
        {
            get { return _Books.Sum(a => a.YearRead); }
        }
        public int TotalToGo
        {
            get
            {
                return _Books.Sum(b => b.ToGo);
            }
        }
        public decimal TotalPercent
        {
            get
            {
                if (TotalPages == 0)
                {
                    return 0;
                }
                else
                {
                    return (decimal)TotalRead / (decimal)TotalPages * 100m;
                }
            }
        }
        public string StatusImage(IBook book)
        {
            if (book.IsFinished && book.PercentRead != 100)
                return "GreyX.gif";
            if (book.PercentRead == 100)
            {
                if (book.LastReadDate.Year == DateTime.Now.Year)
                    return "GreenCheck.png";
                else
                    return "GreyCheck.Gif";
            }
            else if (book.PercentRead > 0)
            {
                int DaysSinceLastRead = ((TimeSpan)DateTime.Now.Subtract(book.LastReadDate)).Days;
                if (DaysSinceLastRead > 21)
                {
                    return "RedX.png";
                }
                else if (DaysSinceLastRead > 21)
                {
                    return "YellowCircle1.png";
                }
                else
                {
                    return "BlueSquareDash.png";
                }
            }
            else
            {
                return "GreySquare.png";
            }
        }
    }
}